package com.wenjh.jdmzt.android.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;

import com.wenjh.jdmzt.R;
import com.wenjh.jdmzt.android.view.ScrollViewPager;

/**
 * 带滚动效果的TabActivity
 */
public class ScrollTabGroupActivity extends BaseActivity {
	private final ArrayList<TabInfo> tabs = new ArrayList<TabInfo>();
	private ScrollViewPager viewPager;
	private boolean pagerTouchScroll = true;
	private Map<Integer, TabOptionFragment> fragments = new HashMap<Integer, TabOptionFragment>();
	private int currentTab = -1;
	
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		viewPager = (ScrollViewPager) findViewById(R.id.tabcontent);
		viewPager.setEnableTouchScroll(pagerTouchScroll);
		
		new TabsAdapter(this, viewPager, tabs);
		
		if (savedInstanceState != null) {
			currentTab = savedInstanceState.getInt("tab");
		}
		
		if(currentTab != -1) {
			viewPager.setCurrentItem(currentTab);
		} else {
			viewPager.setCurrentItem(0);
		}
	}
	
	public TabOptionFragment getFragment(int index) {
		return fragments.get(index);
	}
	
	public void setCurrentTab(int currentTab) {
		this.currentTab = currentTab;
		viewPager.setCurrentItem(currentTab);
	}
	
	public int getCurrentTab() {
		return viewPager.getCurrentItem();
	}
	
	public boolean isPagerTouchScroll() {
		return pagerTouchScroll;
	}
	
	public void setPagerTouchScroll(boolean touchScroll) {
		this.pagerTouchScroll = touchScroll;
		if(viewPager != null) {
			viewPager.setEnableTouchScroll(pagerTouchScroll);
		}
	}
	
	public void addTab(TabInfo tab) {
		tabs.add(tab);
	}

	public void addTab(Class<?>... classes) {
		for (Class<?> clazz : classes) {
			addTab(new TabInfo(clazz));
		}
	}
	
	public int getInitedItemCount() {
		return fragments.size();
	}

	public boolean isAllInited() {
		return getInitedItemCount() == tabs.size();
	}
	
	protected void onTabChanged(Fragment fragment, int position) {
		
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putInt("tab", viewPager.getCurrentItem());
		super.onSaveInstanceState(outState);
	}

	static final class TabInfo {
		private final String tag;
		private final Class<?> clazz;
		private final Bundle args;
		
		public TabInfo(String tag, Class<?> clazz, Bundle args) {
			this.tag = tag;
			this.clazz = clazz;
			this.args = args;
		}

		public TabInfo(String tag, Class<?> clazz) {
			this.tag = tag;
			this.clazz = clazz;
			this.args = null;
		}

		public TabInfo(Class<?> clazz) {
			this.tag = clazz.getName();
			this.clazz = clazz;
			this.args = null;
		}
	}

	public class TabsAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener {
		private final Context context;
		private final ViewPager viewPager;
		private ArrayList<TabInfo> tabs = null;

		public TabsAdapter(FragmentActivity activity,
				ViewPager pager, ArrayList<TabInfo> tabInfos) {
			super(activity.getSupportFragmentManager());
			this.tabs = new ArrayList<TabInfo>();

			context = activity;
			viewPager = pager;

			if (tabInfos != null) {
				for (TabInfo tabInfo : tabInfos) {
					tabs.add(tabInfo);
				}
			}

			viewPager.setOnPageChangeListener(this);
			viewPager.setAdapter(this);
		}

		public void addTab(String tag, Class<?> clss, Bundle args) {
			TabInfo info = new TabInfo(tag, clss, args);
			tabs.add(info);
		}

		@Override
		public int getCount() {
			return tabs.size();
		}

		@Override
		public void startUpdate(ViewGroup container) {
			super.startUpdate(container);
		}
		
		private boolean fristUpdated = true;
		@Override
		public void finishUpdate(ViewGroup container) {
			super.finishUpdate(container);

			if(fristUpdated) {
				fristUpdated = false;
				onPageSelected(viewPager.getCurrentItem());
			}
		}
		
		@Override
		public Fragment getItem(int position) {
			TabInfo info = tabs.get(position);
			TabOptionFragment fragment = (TabOptionFragment) Fragment.instantiate(context, info.clazz.getName(), info.args);
			return fragment;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			Object object = super.instantiateItem(container, position);
			fragments.put(position, (TabOptionFragment)object);
			return object;
		}
		
		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
		}

		@Override
		public void onPageSelected(int position) {
			if(fragments.get(position) != null) {
				ScrollTabGroupActivity.this.onTabChanged(fragments.get(position), position);
			}
		}

		@Override
		public void onPageScrollStateChanged(int state) {
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
		}
	}

	@Override
	protected void initEvents() {
	}

	@Override
	protected void initViews() {
	}
}
